package test3_31.demo01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Homework17_7 {

    /*
    编写一个扑克类(每张扑克有花色和数值，例如红心A,草花10),将52张扑克牌放到集合中(自己去选择集合类)
    方法1.将集合中的所有扑克重新洗牌,并将扑克输出(打印出来即可)
     */
    public static void ShuffleDecks(ArrayList<String> poker) {
        Collections.shuffle(poker);
        System.out.println(poker);
    }


    /*
    将集合中的所有扑克排序(按方块3方块2,草花3草花2, 红心3~红心2, 黑桃3~黑桃2的顺序排序)
     */
    public static class PokerSort implements Comparator<String> {
        public int compare(String p1, String p2) {
            // 定义花色和数值的排序规则
            String[] colorOrder = {"方块", "草花", "红心", "黑桃"};
            String[] numOrder = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

            // 提取花色和数值
            String color1 = p1.substring(0, 2);
            String color2 = p2.substring(0, 2);
            String num1 = p1.substring(2);
            String num2 = p2.substring(2);

            // 比较花色
            int colorIndex1 = getIndex(colorOrder, color1);
            int colorIndex2 = getIndex(colorOrder, color2);
            if (colorIndex1 != colorIndex2) {
                return Integer.compare(colorIndex1, colorIndex2);
            }

            // 比较数值
            int rankIndex1 = getIndex(numOrder, num1);
            int rankIndex2 = getIndex(numOrder, num2);
            return Integer.compare(rankIndex1, rankIndex2);
        }

        private int getIndex(String[] arr, String value) {
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].equals(value)) {
                    return i;
                }
            }
            return -1;
        }
    }

    /*
    比较两张扑克牌的大小，如果版面的值相等，则按（黑桃->红心->草花->方块）的顺序进行排列
     */

    public static void comparePoker(String p1, String p2) {

        String[] colorOrder = {"黑桃", "红心", "草花", "方块"};
        String[] numOrder = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

        // 提取花色和数值
        String color1 = p1.substring(0, 2);
        String color2 = p2.substring(0, 2);
        String num1 = p1.substring(2);
        String num2 = p2.substring(2);

        int rankIndex1 = getIndex(numOrder, num1);
        int rankIndex2 = getIndex(numOrder, num2);
        if (rankIndex1 < rankIndex2){
            System.out.println(p1 +"小于"+p2);
        }else if(rankIndex1 > rankIndex2){
            System.out.println(p1 +"大于"+p2);
        }else {
            int colorIndex1 = getIndex(colorOrder, color1);
            int colorIndex2 = getIndex(colorOrder, color2);
            if(colorIndex1<colorIndex2){
                System.out.println(p1 +"大于"+p2);
            }else if(colorIndex1 > colorIndex2){
                System.out.println(p1 +"小于"+p2);
            }else {
                System.out.println("两张牌相等");
            }
        }

    }
    private static int getIndex(String[] arr, String value) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(value)) {
                return i;
            }
        }
        return -1;
    }


    public static void main(String[] args) {

        String[] color = new String[]{"黑桃", "草花", "方块", "红心"};
        String[] num = new String[]{"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        ArrayList<String> poker = new ArrayList<>();
        for (int i = 0; i < color.length; i++) {
            for (int j = 0; j < num.length; j++) {
                poker.add(color[i] + num[j]);
            }
        }

        Homework17_7.ShuffleDecks(poker);

        Collections.sort(poker, new PokerSort());
        System.out.println(poker);

        Homework17_7.comparePoker("草花3","草花A");


    }

}
